﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;

namespace YArchitech.HVAC
{
	public class IndoorSystem
	{
		public IndoorSystem(int num)
		{
			this.index = num;
			this.loops = new List<IndoorLoop>();
		}

		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				this.index = value;
			}
		}

		public string Name
		{
			get
			{
				return "分户" + this.index;
			}
		}

		public IList<IndoorLoop> Loops
		{
			get
			{
				return this.loops;
			}
		}

		public HyCalcPipe SupplyPipe
		{
			get
			{
				return this.supplyPipe;
			}
			set
			{
				this.supplyPipe = value;
			}
		}

		public HyCalcPipe BackPipe
		{
			get
			{
				return this.backPipe;
			}
			set
			{
				this.backPipe = value;
			}
		}

		public double Resistance
		{
			get
			{
				return this.resistance;
			}
		}

		public double RateUnbalance
		{
			get
			{
				return this.rateUnbalance;
			}
			set
			{
				this.rateUnbalance = value;
			}
		}

		public IList<Element> Elements
		{
			get
			{
				return this.GetAllElements();
			}
		}

		public double AvailPress { get; set; }

		public int WorstIndex
		{
			get
			{
				return this.GetWorstLoopIndex();
			}
		}

		public void Add(IndoorLoop loop)
		{
			this.loops.Add(loop);
		}

		public IndoorLoop GetAt(int loopIndex)
		{
			return (from loop in this.loops
			where loop.Index == loopIndex
			select loop).First<IndoorLoop>();
		}

		private int GetWorstLoopIndex()
		{
			this.worstIndex = (from loop in this.loops
			where loop.Resistance == (from lp in this.loops.AsEnumerable<IndoorLoop>()
			select lp.Resistance).Max()
			select loop.Index).First<int>();
			return this.worstIndex;
		}

		public double CalcResistance()
		{
			this.resistance = (from loop in this.loops
			select loop.CalcResistance()).Max();
			if (this.supplyPipe != null && this.backPipe != null)
			{
				this.resistance += this.supplyPipe.Resistance + this.backPipe.Resistance;
			}
			Log.Info(new string[]
			{
				this.Name + ": resistance = " + this.resistance
			});
			return this.resistance;
		}

		public Dictionary<string, double> CalaUnbalance()
		{
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			double num = (from loop in this.loops.AsEnumerable<IndoorLoop>()
			select loop.Resistance).Max();
			foreach (IndoorLoop indoorLoop in this.loops)
			{
				indoorLoop.CalcUnbalanceRate();
				double num2 = Math.Abs((indoorLoop.Resistance - num) / num);
				indoorLoop.RateUnbalance = num2;
				dictionary.Add(indoorLoop.Name, num2);
				Log.Info(new string[]
				{
					string.Format("System {0} Resistance = {1} Unbalance={2}", indoorLoop.Name, indoorLoop.Resistance, num2)
				});
			}
			return dictionary;
		}

		private IList<Element> GetAllElements()
		{
			List<Element> list = new List<Element>();
			if (this.supplyPipe != null && this.backPipe != null)
			{
				list.AddRange(this.supplyPipe.Elements);
				list.AddRange(this.backPipe.Elements);
			}
			foreach (IndoorLoop indoorLoop in this.loops)
			{
				list.AddRange(indoorLoop.Elements);
			}
			return list;
		}

		public TreeNode ToTreeNode()
		{
			TreeNode treeNode = new TreeNode(this.Name);
			treeNode.Tag = this;
			if (this.supplyPipe != null && this.backPipe != null)
			{
				TreeNode treeNode2 = new TreeNode("户内供水干管");
				treeNode2.Tag = this.SupplyPipe;
				TreeNode treeNode3 = new TreeNode("户内回水干管");
				treeNode3.Tag = this.BackPipe;
				treeNode.Nodes.AddRange(new TreeNode[]
				{
					treeNode2,
					treeNode3
				});
			}
			treeNode.Nodes.AddRange((from loop in this.loops
			select loop.ToTreeNode()).ToArray<TreeNode>());
			return treeNode;
		}

		public void UpDate()
		{
			if (this.supplyPipe != null)
			{
				this.supplyPipe.GetAllParam();
				this.backPipe.GetAllParam();
			}
			this.loops.ForEach(delegate(IndoorLoop loop)
			{
				loop.UpDate();
			});
		}

		public void BackRevit()
		{
			if (this.supplyPipe != null)
			{
				this.supplyPipe.BackRevit();
				this.backPipe.BackRevit();
			}
			this.loops.ForEach(delegate(IndoorLoop loop)
			{
				loop.BackRevit();
			});
		}

		private int index = 1;

		private IList<IndoorLoop> loops;

		private double resistance;

		private double rateUnbalance;

		private HyCalcPipe supplyPipe;

		private HyCalcPipe backPipe;

		private int worstIndex = 1;
	}
}
